package rbac.View.Main.View;

import rbac.View.Main.DoMain.Vo.RoleVo;
import rbac.View.Main.DoMain.Vo.Result;
import rbac.View.Main.Service.IRoleService;
import rbac.View.Main.DoMain.Dto.RoleDto;
import rbac.View.Main.Service.array.RoleService;

public class RoleView extends BaseView<RoleVo, IRoleService> {
    private static final String MENU = """
            --------------
                RBAC系统
            --------------
             | 0 返回上级 |
             | 1 角色列表 |
             | 2 创建角色 |
             | 3 更新角色 |
             | 4 删除角色 |
            --------------
            请输入选项：
            """;
    private static RoleView singleton;

    private RoleView() {
        service = RoleService.getInstance();
    }

    public static RoleView getInstance() {
        if (singleton == null) {
            singleton = new RoleView();
        }
        return singleton;
    }

    public void show() {
        String menuChoice;
        do {
            System.out.println(MENU);
            menuChoice = View.input.nextLine();
            switch (menuChoice) {
                case "0" -> System.out.println("已返回上级菜单！");
                case "1" -> list();
                case "2" -> create();
                case "3" -> update();
                case "4" -> delete();
                default -> System.out.println("输入非法，请输入正确的菜单编号！");
            }
        } while (!"0".equals(menuChoice));
        View.input.close();
    }

    public void list() {
        Result result = service.list();
        cache = (RoleVo[]) result.data;
        System.out.println("ID\t角色名称\t角色数据访问范围\t");
        for (RoleVo rolevo : cache) {
            System.out.printf("%d\t%s\t%s\t\n", rolevo.getId(), rolevo.name, rolevo.dataScope);
        }
    }

    public void create() {
        System.out.println("请输入角色名称:");
        String name = View.input.nextLine();
        System.out.println("请输入角色数据访问范围，输入的值应该为【个人、部门、全局】其中之一:");
        String dataScope = null;
        do {
            String str = View.input.nextLine();
            if ("个人".equals(str) || "部门".equals(str) || "全局".equals(str)) {
                dataScope = str;
            } else {
                System.out.print("输入非法，输入的值应该为【个人、部门、全局】其中之一，请输入正确的数据访问范围！");
            }
        } while (dataScope == null);
        RoleDto dto = new RoleDto(name, dataScope);
        Result result = service.create(dto);
        if (!result.success) {

            printError(result.message);
            return;
        }
        System.out.println(result.message);
        list();
    }

    public void update() {
        RoleVo role = getCacheById();

        if (role == null) {
            return;
        }

        System.out.println("请输入角色名称，直接按回车表示不修改：");
        String name = View.input.nextLine();
        name = "".equals(name) ? role.name : name;
        System.out.println("请输入角色数据访问范围，输入的值应该为【个人、部门、全局】其中之一:");
        String dataScope = null;
        do {
            String str = View.input.nextLine();
            if ("个人".equals(str) || "部门".equals(str) || "全局".equals(str)) {
                dataScope = str;
            } else if ("".equals(str)) {
                dataScope = role.dataScope;
            } else {
                System.out.print("输入非法，输入的值应该为【个人、部门、全局】其中之一，请输入正确的数据访问范围！");
            }
        } while (dataScope == null);
        RoleDto dto = new RoleDto(name, dataScope);
        Result result = service.update(dto, role.getId());
        if (!result.success) {
            printError(result.message);
            return;
        }

        System.out.println(result.message);
        list();
    }

    public void delete() {
        RoleVo role = getCacheById();

        if (role == null) {
            return;
        }

        System.out.println("输入'Y'确认删除，输入其他则取消删除！");
        String confirm = View.input.nextLine();

        if ("Y".equalsIgnoreCase(confirm)) {
            System.out.println("您已取消删除！");
            return;
        }

        Result result = service.delete(role.getId());

        if (!result.success) {
            printError(result.message);
            return;
        }
        System.out.println(result.message);
        list();
    }
}

